import { AIPropertyDescriptor, IField } from "../AIProperty";
import { Field } from "./Field";

export class Vec2Field extends Field implements IField {
    protected _defaultData = { x: 0, y: 0 };
    protected _widget = 'Vec2';
    create(o: AIPropertyDescriptor, data: any) {
        let p = this._create(o, this._widget, data, this._defaultData);
        let val = this._panel.getChild("value");
        let x = val.getChild('x') as IEditor.NumericInput;
        let y = val.getChild('y') as IEditor.NumericInput;

        if (o.step) {
            x.step = o.step;
            y.step = o.step;
        }
        if (null != o.min) {
            x.min = o.min;
            y.min = o.min;
        }
        if (null != o.max) {
            x.max = o.max;
            y.max = o.max;
        }

        x.on("submit", this._changeHandle, this);
        y.on("submit", this._changeHandle, this);
        return p;
    }
    protected _changeHandle(e: gui.Event) {
        let vec2 = this._panel.getChild("value");
        let x = vec2.getChild('x');
        let y = vec2.getChild('y');

        let o = this.data[this.o.name];
        o.x = Number(x.text);
        o.y = Number(y.text);
    }
    protected _setValue(val: any) {
        let vec2 = this._panel.getChild("value");
        let x = vec2.getChild('x');
        let y = vec2.getChild('y');
        x.text = val.x;
        y.text = val.y;
    }
}